﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class DeleteCreditCard : Form
    {
        ArrayList passCustomerDetails;
        Customer passSingleCustomerDetail = new Customer();
        Int64 storeCardNum;
        public DeleteCreditCard()
        {
            InitializeComponent();
            ArrayList getCustomerInfo = ManageCustomerDBManager.GetAllCustomerDetails();
            foreach (Customer c in getCustomerInfo)
            {
                cboxnric.Items.Add(c.Nric + ", " + c.CustomerID);
            }
            passCustomerDetails = getCustomerInfo;
        }

        private void cboxnric_SelectedIndexChanged(object sender, EventArgs e)
        {
             foreach(Customer c in passCustomerDetails)
             {
                 if (ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID).Count == 0 && cboxnric.SelectedItem.ToString().Contains(c.CustomerID))
                 {
                     MessageBox.Show("No Credit Card Info");
                     cboxcnumber.Items.Clear();
                     cboxcnumber.ResetText();
                     lblshowcontactno.Text = "-";
                     lblshowname.Text = "-";
                     lblshowcreditcardholdername.Text = "-";
                     lblshowcustomerid.Text = "-";
                     lblshowcreditcardexpdate.Text = "";
                     lblshowcreditcardnumber.Text = "";
                     return;
                 }

                 if (ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID).Count > 0 && cboxnric.SelectedItem.ToString().Contains(c.CustomerID))
                 {
                     cboxcnumber.Items.Clear();
                     cboxcnumber.ResetText();
                     ArrayList tempCreditCardInfo = new ArrayList();
                     tempCreditCardInfo = ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID);
                     foreach (CreditCardInfo cCardInfo in tempCreditCardInfo)
                     {
                         cboxcnumber.Items.Add(cCardInfo.CreditCardNo);
                     }
                     lblshowcontactno.Text = c.ContactNo.ToString();
                     lblshowname.Text = c.FirstName + " " + c.LastName;
                     passSingleCustomerDetail.CustomerID = c.CustomerID;
                     passSingleCustomerDetail.FirstName = c.FirstName;
                     passSingleCustomerDetail.LastName = c.LastName;
                     lblshowcreditcardholdername.Text = "-";
                     lblshowcustomerid.Text = "-";
                     lblshowcreditcardexpdate.Text = "";
                     lblshowcreditcardnumber.Text = "";
                     return;
                 }
             }
        }

        private void cboxcnumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblshowcreditcardholdername.Text = passSingleCustomerDetail.FirstName + " " + passSingleCustomerDetail.LastName;
            lblshowcustomerid.Text = passSingleCustomerDetail.CustomerID;
            ArrayList getCreditCardInfo = ManageCustomerDBManager.GetCreditInfoByCustomerId(passSingleCustomerDetail.CustomerID);
            foreach(CreditCardInfo cardInfo in getCreditCardInfo)
            {
                if(cboxcnumber.SelectedItem.ToString().Equals(cardInfo.CreditCardNo.ToString()))
                {
                    lblshowcreditcardnumber.Text = cardInfo.CreditCardNo.ToString();
                    lblshowcreditcardexpdate.Text = cardInfo.CreditCardExpDate.ToShortDateString();
                    storeCardNum = cardInfo.CreditCardNo;
                }
            }
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            if(cboxcnumber.SelectedIndex.Equals(-1) || cboxnric.SelectedIndex.Equals(-1))
            {
                MessageBox.Show("Please check whether you have selected any card or nric details");
                return;
            }
            
            if (MessageBox.Show("Do you want to delete credit card info?", "Confirm Delete?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if(ManageCustomerDBManager.DeleteCreditCardInfoByCardNum(storeCardNum) == 1)
                {
                    MessageBox.Show("Deleted Sucessfully");
                    this.Hide();
                }
            }
        }



    }
}

